package com.quasar.hpatient.module.mine_case;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.ChooseSingleDialog;
import com.quasar.hpatient.dialog.ChooseTimeDialog;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.module.mine_case_edit.MineCaseEditActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.photo.PhotoLayout;
import lib.quasar.widget.photo.PhotoModel;

/**
 * description: 我 ==> 我的档案 ==> 我的病例
 * created by kalu on 2018/7/16 17:14
 */
public final class MineCaseActivity extends BaseActivity<MineCasePresenter> implements MineCaseView {

    public final static int REQUEST_CODE = 1111;

    @Override
    public int initView() {
        return R.layout.activity_mine_case;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.mine_case_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else goNextResult(MineCaseEditActivity.class, REQUEST_CODE);
        });

        final TextView date = findViewById(R.id.mine_case_search);
        date.setOnClickListener(v -> showSearch(date));

        final RecyclerView recycler = findViewById(R.id.mine_case_list);
        getPresenter().initList(this, recycler);
    }

    @Override
    public void initDataNet() {

        final RecyclerView recycler = findViewById(R.id.mine_case_list);
        getPresenter().caseList(this, recycler, null, null);
    }

    @Override
    public void showPhoto(int defaultPosition, List<? extends PhotoModel> imageLittleUrlList, List<? extends PhotoModel> imageUrlList, List<ImageView> imageViews) {

        if (imageLittleUrlList.isEmpty() || imageUrlList.isEmpty() || imageViews.isEmpty())
            return;

        new PhotoLayout.Builder(this)
                .setPhotoLongPressSave(false)
                .setPhotoOpenTransAnim(false)
                .setPhotoDefaultPosition(defaultPosition)
                .setPhotoBackgroundColor(Color.BLACK)
                .setPhotoDefaultResource(R.mipmap.ic_launcher)
                .setPhotoViewList(imageViews)
                .setPhotoUrlList(imageUrlList)
                .setPhotoLittleUrlList(imageLittleUrlList)
                .show();
    }

    @Override
    public void showSearch(TextView date) {

        final ChooseSingleDialog dialog = new ChooseSingleDialog(this);
        dialog.show();
        dialog.setList(Arrays.asList("全部", "一周", "两周", "一月", "三月", "半年", "自定义"));
        dialog.setOnDialogChangeListener(message -> {
            date.setText(message);
            final RecyclerView recycler = findViewById(R.id.mine_case_list);
            if ("自定义".equals(message)) {
                final ChooseTimeDialog length = new ChooseTimeDialog(MineCaseActivity.this);
                length.show();
                length.setTop("开始时间");
                length.setBottom("结束时间");
                length.setOnDialogChangeListener(new ChooseTimeDialog.OnDialogChangeListener() {
                    @Override
                    public void onChange(String begin, String end) {
                        final RecyclerView recycler = findViewById(R.id.mine_case_list);
                        getPresenter().caseList(MineCaseActivity.this, recycler, begin, end);
                    }

                    @Override
                    public void onTop() {

                        final DateDialog date = new DateDialog(MineCaseActivity.this);
                        date.show();
                        date.setOnDateChangeListener((year, month, day) -> length.setTop(year + "-" + month + "-" + day));
                    }

                    @Override
                    public void onBottom() {
                        final DateDialog date = new DateDialog(MineCaseActivity.this);
                        date.show();
                        date.setOnDateChangeListener((year, month, day) -> length.setBottom(year + "-" + month + "-" + day));
                    }
                });
            } else {
                getPresenter().caculDate(MineCaseActivity.this, recycler, message);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode != REQUEST_CODE || resultCode != MineCaseEditActivity.RESULT_CODE || null == data)
            return;

        final int caseId = data.getIntExtra(MineCaseEditActivity.CASE_ID, -1);
        final String caseTitle = data.getStringExtra(MineCaseEditActivity.CASE_TITLE);
        final String caseDate = data.getStringExtra(MineCaseEditActivity.CASE_DATE);
        final String caseName = data.getStringExtra(MineCaseEditActivity.CASE_NAME);
        final String caseInspection = data.getStringExtra(MineCaseEditActivity.CASE_INSPECTION);
        final ArrayList<String> caseList = data.getStringArrayListExtra(MineCaseEditActivity.CASE_PICTURE);
        final RecyclerView recycler = findViewById(R.id.mine_case_list);
        getPresenter().refreshDate(this, recycler, caseList, caseId, caseTitle, caseName, caseDate, caseInspection);
    }
}